package com.zhixi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhixi.common.CategoryException;
import com.zhixi.pojo.Category;
import com.zhixi.pojo.Dish;
import com.zhixi.pojo.Setmeal;
import com.zhixi.service.CategoryService;
import com.zhixi.mapper.CategoryMapper;
import com.zhixi.service.DishService;
import com.zhixi.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhangzhixi
 * @description 针对表【category(菜品及套餐分类)】的数据库操作Service实现
 * @createDate 2022-07-10 13:38:41
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
        implements CategoryService {

    /**
     * 菜品Service
     */
    @Autowired
    private DishService dishService;

    /**
     * 套餐Service
     */
    @Autowired
    private SetmealService setmealService;

    /**
     * 根据id移除菜品
     * @param id 菜品id
     */
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishQuery = new LambdaQueryWrapper<>();
        dishQuery.eq(Dish::getCategoryId, id);
        long dishCount = dishService.count(dishQuery);

        if (dishCount > 0) {
            throw new CategoryException("该分类下存在菜品，不能删除");
        }

        LambdaQueryWrapper<Setmeal> setmealQuery = new LambdaQueryWrapper<>();
        setmealQuery.eq(Setmeal::getCategoryId, id);
        long setmealCount = setmealService.count(setmealQuery);
        if (setmealCount > 0) {
            throw new CategoryException("该分类下存在套餐，不能删除");
        }
        super.removeById(id);
    }
}




